<?php

namespace Admin\Controller;
use Think\Controller;

class BonusConfigController extends AdminController
{
	public function index()
	{
		//$map['status']=array('egt','0');
		$data_list=D("BonusConfig")->page(!empty($_GET['p'])?$_GET['p']:1,C("ADMIN_PAGE_ROWS"))->order("id desc")->select();
		$page=new \Common\Util\Page(D("BonusConfig")->count(),C("ADMIN_PAGE_ROWS"));
		//用builder快速建立列表
		$builder=new \Common\Builder\ListBuilder();
		$builder->setMetaTitle('奖金配置列表') //设置页面标题
		->addTopButton('addnew')  //添加新增按钮
// 		->addTopButton('resume')  //添加启用按钮
// 		->addTopButton('forbid')  //添加禁用按钮
		->addTopButton('delete')  //添加删除按钮
		->addTableColumn('id', 'ID')
		->addTableColumn('name', '名称')
		->addTableColumn('config_type', '配置类型', '')
		->addTableColumn('min_layer', '最小', '')
		->addTableColumn('max_layer', '最大', '')
		->addTableColumn('value', '配置值')
		->addTableColumn('ext', '其他')
		->addTableColumn('right_button', '操作', 'btn')
		->setTableDataList($data_list) //数据列表
		->setTableDataPage($page->show()) //数据列表分页
		->addRightButton('edit')   //添加编辑按钮
		->addRightButton('forbid') //添加禁用/启用按钮
		->addRightButton('delete') //添加删除按钮
		->display();
		
	}
	public function add()
	{
		if(IS_POST)
		{
			$bonus_object=D("BonusConfig");
			$data=$bonus_object->create();
			if($data)
			{
				$id=$bonus_object->add();
			
				if($id){
					
					$this->success("添加成功",U("index"));
				}else{
					$this->error("添加失败");
				}
			}else{
				$this->error($bonus_object->getError());
			}
		}else{
			
			$bonus_config = get_descriptions_to_option('BonusConfig','ConfigType');
			
			$builder=new \Common\Builder\FormBuilder();
			$builder->setMetaTitle('新增奖金配置') //设置页面标题
			->setPostUrl(U('add')) //设置表单提交地址
			->addFormItem('name', 'text', '名称', '配置名称')
			->addFormItem('config_type', 'select', '配置类型', '配置类型',$bonus_config)
			->addFormItem('min_layer', 'text', '最小层', '最小层')
			->addFormItem('max_layer', 'text', '最大层', '最大层')
			->addFormItem('value', 'text', '配置值', '配置值')
			->addFormItem('ext', 'text', '其他', '')
			->display();
		} 
	} 
	public function edit()
	{
		$uid=is_login();
		if(IS_POST)
			{
				$Bonus_object=D("BonusConfig");
				$data=$Bonus_object->create();
				
				if($data)
				{
					$id=$Bonus_object->save();
					if($id){
						
						$this->success("修改成功",U("index"));
					}else{
						$this->error("修改失败");
					}
				}else{
					$this->error("修改失败",$Bonus_object->getError());
				}
			}else{
				$id=I("request.id");
				//echo $id;
				
				
				$bonus_config = get_descriptions_to_option('BonusConfig','ConfigType');
			
				$builder=new \Common\Builder\FormBuilder();
				$builder->setMetaTitle('编辑奖金类型') //设置页面标题
				->setPostUrl(U('edit')) //设置表单提交地址
				->addFormItem('id', 'hidden', 'ID','ID')
				->addFormItem('name', 'text', '名称', '配置名称')
				->addFormItem('config_type', 'select', '配置类型', '配置类型',$bonus_config)
				->addFormItem('min_layer', 'text', '最小层', '最小层')
				->addFormItem('max_layer', 'text', '最大层', '最大层')
				->addFormItem('value', 'text', '配置值', '配置值')
				->addFormItem('ext', 'text', '其他', '')
 				->setFormData(D("BonusConfig")->find($id))
				->display();
			}
	}
}